#include "CreateChannelWindow.h"
#include "main.h"

CreateChannelWindow::CreateChannelWindow(QWidget *parent) :
_parent(parent)
{
	setWindowIcon(QIcon("./images/Agora.png"));
	display();
	this->show();
}

void CreateChannelWindow::keyPressEvent(QKeyEvent *e)
{
	if (e->key() == Qt::Key_Return || e->key() == Qt::Key_Enter)
		createChannel();
	QWidget::keyPressEvent(e);
}

void		CreateChannelWindow::display()
{
	this->setMinimumSize(320, 250);
	this->setMaximumSize(320, 250);
			//IP GroupBox
	QGroupBox *chanInfoBox = new QGroupBox(this);
	chanInfoBox->setTitle("Channel's Informations");
	chanInfoBox->setGeometry(10, 10, 300, 200);
	QVBoxLayout	*chanInfoVBox = new QVBoxLayout();

	QLabel *chanNameLabel = new QLabel(tr("Name : "));
	_chanName = new QLineEdit();
	QHBoxLayout *chanNameHBox = new QHBoxLayout();
	chanNameHBox->addWidget(chanNameLabel);
	chanNameHBox->addWidget(_chanName);
	QLabel *chanDescLabel = new QLabel(tr("Description : "));
	_chanDesc = new QLineEdit();
	QHBoxLayout *chanDescHBox = new QHBoxLayout();
	chanDescHBox->addWidget(chanDescLabel);
	chanDescHBox->addWidget(_chanDesc);
	QLabel *chanMOTDLabel = new QLabel(tr("MOTD : "));
	_chanMOTD = new QLineEdit();
	QHBoxLayout *chanMOTDHBox = new QHBoxLayout();
	chanMOTDHBox->addWidget(chanMOTDLabel);
	chanMOTDHBox->addWidget(_chanMOTD);
	QLabel *chanPassLabel = new QLabel(tr("Password : "));
	_chanPass = new QLineEdit();
	_chanPass->setEchoMode(QLineEdit::Password);
	QHBoxLayout *chanPassHBox = new QHBoxLayout();
	chanPassHBox->addWidget(chanPassLabel);
	chanPassHBox->addWidget(_chanPass);
	QLabel *chanConPassLabel = new QLabel(tr("Confirm Password : "));
	_chanConPass = new QLineEdit();
	_chanConPass->setEchoMode(QLineEdit::Password);
	QHBoxLayout *chanConPassHBox = new QHBoxLayout();
	chanConPassHBox->addWidget(chanConPassLabel);
	chanConPassHBox->addWidget(_chanConPass);

	chanInfoVBox->addLayout(chanNameHBox);
	chanInfoVBox->addLayout(chanDescHBox);
	chanInfoVBox->addLayout(chanMOTDHBox);
	chanInfoVBox->addLayout(chanPassHBox);
	chanInfoVBox->addLayout(chanConPassHBox);
	chanInfoBox->setLayout(chanInfoVBox);

	QPushButton* buttonAdd = new QPushButton("Add", this);
	buttonAdd->setGeometry(10, 215, 300, 30);

	connect(buttonAdd, SIGNAL(clicked()), this, SLOT(createChannel()));
}

void CreateChannelWindow::createChannel()
{
	if (_chanPass->text() == _chanConPass->text())
	{
		dynamic_cast<Main*>(this->_parent)->createChannel(_chanName->text().toStdString(),
			_chanPass->text().toStdString(),
			_chanMOTD->text().toStdString(),
			_chanDesc->text().toStdString(),
			_parentChannelId);
		this->hide();
	}
	else
		QMessageBox::information(0, "Error", "You have made some mistakes in the password");
}

void CreateChannelWindow::setParentChannelId(int parentChannelId)
{
	_parentChannelId = parentChannelId;
}